package com.site.wisdom_construction.controller.login;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.site.wisdom_construction.entity.personnelModule.ContractWorkers;
import com.site.wisdom_construction.entity.personnelModule.ManagerialStaff;
import com.site.wisdom_construction.entity.userAuthorityModule.User;
import com.site.wisdom_construction.responseEntity.LoginResult;
import com.site.wisdom_construction.responseEntity.UserLoginResult;
import com.site.wisdom_construction.service.personnelModule.ContractWorkersService;
import com.site.wisdom_construction.service.personnelModule.ManagerialStaffService;
import com.site.wisdom_construction.service.userAuthorityModule.UserService;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/** @Author daiyifei @Date 2021/12/22 @Description */
@Api(tags = "登录接口")
@RestController
@RequestMapping("Login")
@CrossOrigin
public class LoginController {

  @Autowired private UserService userService;

  @Autowired private ManagerialStaffService managerialStaffService;

  @Autowired
  private ContractWorkersService contractWorkersService;

  @NoToken
  @ApiOperation(value = "登录接口", notes = "测试数据:账号(contactsPhone),密码(password)")
  @PostMapping("/backgroundLogin")
  public LoginResult backgroundLogin(@RequestBody Map map) {

    LoginResult loginResult = new LoginResult();
    // 根据用户名查询用户数据
    User user = userService.findUserByContactsPhone(map);

    // 验证用户是否存在
    if (user != null) {
      // 封装返回实体类
      UserLoginResult userLoginResult = new UserLoginResult();
      userLoginResult.setId(user.getId());
      userLoginResult.setAccount(user.getAccount());
      userLoginResult.setContactsName(user.getContactsName());
      userLoginResult.setContactsPhone(user.getContactsPhone());
      userLoginResult.setProjectId(user.getProjectId());
      userLoginResult.setProjectEndTime(user.getProjectEndTime());
      userLoginResult.setProjectStartTime(user.getProjectStartTime());
      userLoginResult.setFatherId(user.getFatherId());
      userLoginResult.setIsparent(user.getIsparent());
      userLoginResult.setLeave(user.getLeave());
      userLoginResult.setStatus(user.getStatus());
      userLoginResult.setProjectName(user.getProjectName());

      // 验证账号是否可用(到期)
      if (user.getStatus().equals("1")) {
        // 验证密码是否正确
        if ((map.get("password").equals(user.getPassword()))) {
          String token =
              JWT.create()
                  .withAudience(user.getId() + "")
                  .sign(Algorithm.HMAC256(user.getPassword()));
          loginResult.setCode(200);
          loginResult.setMessage("登录成功!");
          loginResult.setResult(userLoginResult);
          loginResult.setToken(token);
        } else {
          loginResult.setCode(20002);
          loginResult.setMessage("密码错误,请重新输入密码");
        }

      } else {
        loginResult.setCode(20001);
        loginResult.setMessage("账号已过期,请通知管理员");
      }

    } else {
      loginResult.setCode(20000);
      loginResult.setMessage("账号不存在");
    }
    return loginResult;
  }

  @NoToken
  @ApiOperation(value = "安管人员登录接口", notes = "测试数据:账号(managerialPhone),密码(password)")
  @PostMapping("/managerialLogin")
  public LoginResult managerialLogin(@RequestBody Map map) {
    LoginResult loginResult = new LoginResult();
    ManagerialStaff managerialStaff = managerialStaffService.findManagerialStaffByManagerialPhone(map);
    map.put("projectId",managerialStaff.getProjectId());

    if (managerialStaff != null) {
      User user = userService.findUserByProjectIdOne(map);
      if ((map.get("password").equals(managerialStaff.getPassword()))) {
        String token =
            JWT.create()
                .withAudience(user.getId() + "")
                .sign(Algorithm.HMAC256(user.getPassword()));
        loginResult.setCode(200);
        loginResult.setMessage("登录成功!");
        loginResult.setResult(managerialStaff);
        loginResult.setToken(token);
      } else {
        loginResult.setCode(20002);
        loginResult.setMessage("密码错误,请重新输入密码");
      }
    } else {
      loginResult.setCode(20000);
      loginResult.setMessage("账号不存在");
    }
    return loginResult;
  }

  @NoToken
  @ApiOperation(value = "劳务人员登录接口", notes = "测试数据:账号(workerPhone),密码(password)")
  @PostMapping("/contractWorkersLogin")
  public LoginResult contractWorkersLogin(@RequestBody Map map) {
    // managerial_phone
    LoginResult loginResult = new LoginResult();
    ContractWorkers contractWorkers = contractWorkersService.findContractWorkersByWorkerPhone(map);
    map.put("projectId",contractWorkers.getProjectId());
    if (contractWorkers != null) {
      User user = userService.findUserByProjectIdOne(map);
      if ((map.get("password").equals(contractWorkers.getPassword()))) {
        String token =
                JWT.create()
                        .withAudience(user.getId() + "")
                        .sign(Algorithm.HMAC256(user.getPassword()));
        loginResult.setCode(200);
        loginResult.setMessage("登录成功!");
        loginResult.setResult(contractWorkers);
        loginResult.setToken(token);
      } else {
        loginResult.setCode(20002);
        loginResult.setMessage("密码错误,请重新输入密码");
      }
    } else {
      loginResult.setCode(20000);
      loginResult.setMessage("账号不存在");
    }
    return loginResult;
  }
}
